package com.codefish.framework.spring.aop.aspectj;

import com.codefish.framework.spring.aop.IPointcut;
import com.codefish.framework.spring.aop.IPointcutAdvisor;
import org.aopalliance.aop.Advice;

/**
 * 支持AspectJ表达式的PointcutAdvisor
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/10/31 下午 08:15
 */
public class AspectJExpressionPointcutAdvisor implements IPointcutAdvisor {

    private AspectJExpressionPointcut pointcut;
    private Advice advice;
    private String expression;

    public AspectJExpressionPointcutAdvisor() {

    }


    public void setPointcut(String expression) {
        this.expression = expression;
        this.pointcut = null;
    }


    public void setAdvice(Advice advice) {
        this.advice = advice;
    }

    public AspectJExpressionPointcutAdvisor(String expression, Advice advice) {
        this.expression = expression;
        this.advice = advice;
    }

    @Override
    public IPointcut getPointcut() {
        refreshPointcut();
        return this.pointcut;
    }

    @Override
    public Advice getAdvice() {
        return this.advice;
    }

    private void refreshPointcut() {
        if (this.pointcut == null) {
            this.pointcut = new AspectJExpressionPointcut(expression);
        }
    }
}
